package com.eco.netty;

import com.alibaba.fastjson.JSONArray;
import com.eco.utils.DateAndHexUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/* loaded from: newfishway-1.0.0-SNAPSHOT.jar:BOOT-INF/classes/com/eco/netty/Sensor3Decoder.class */
public class Sensor3Decoder extends ByteToMessageDecoder {
    private static final Logger log = LoggerFactory.getLogger((Class<?>) Sensor3Decoder.class);
    public static int BASE_LENTH = 25;
    private String cksw1;
    private String ckls1;

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
        try {
            if (in.readableBytes() > 2048) {
                in.skipBytes(in.readableBytes());
            }
            JSONArray result = byteBuf2String(in);
            if (result == null) {
                return;
            }
            out.add(result);
        } catch (Exception e) {
            log.error("自定义解码器异常:{}", e.toString());
        }
    }

    private JSONArray byteBuf2String(ByteBuf byteBuf) throws Exception {
        JSONArray sensor3Data = new JSONArray();
        int endReader = byteBuf.readableBytes();
        byte[] useless = new byte[2];
        byte[] para = new byte[4];
        byteBuf.readBytes(useless);
        byteBuf.readByte();
        byteBuf.readBytes(para);
        String cksw = DateAndHexUtil.byte2FloatStr(para);
        if (NumberUtils.isCreatable(cksw) && Float.parseFloat(cksw) >= 0.0f && Float.parseFloat(cksw) < 10.0f) {
            this.cksw1 = cksw;
        } else {
            log.error("cksw值异常:{}", cksw);
            if (this.cksw1 != null) {
                cksw = this.cksw1;
            } else {
                cksw = "0.01";
            }
        }
        sensor3Data.add(cksw);
        byteBuf.readBytes(para);
        String ckls = DateAndHexUtil.byte2FloatStr(para);
        if (NumberUtils.isCreatable(ckls) && Float.parseFloat(ckls) >= 0.0f && Float.parseFloat(ckls) < 2.0f) {
            this.ckls1 = ckls;
        } else {
            log.error("ckls值异常:{}", ckls);
            if (this.ckls1 != null) {
                ckls = this.ckls1;
            } else {
                ckls = "0.01";
            }
        }
        sensor3Data.add(ckls);
        byteBuf.readBytes(para);
        byteBuf.readBytes(para);
        byteBuf.readBytes(para);
        byteBuf.readBytes(useless);
        byteBuf.readerIndex(endReader);
        return sensor3Data;
    }
}